﻿Public Class Form1

    'buat di baca 
    'ini ta bikin 30 level
    'per 10 level ada tambahan tanda
    'per 1 level selesai + 20 detik
    Dim gameOngoing As Boolean = False
    Dim waitingAnswer As Boolean = True
    Dim gameLevel As Integer = 0
    Dim answer As Integer = 0
    Dim q1 As Integer = 0
    Dim q2 As Integer = 0
    Dim marks As Char = ""    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        waitingAnswer = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If TimeLabel.Text <= 0 Then
            gameEnds()
        Else
            TimeLabel.Text -= 1
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        gameStart()
    End Sub

    Private Sub gameStart()
        waitingAnswer = True
        TimeLabel.Text = "50"
        Button1.Enabled = False
        Timer1.Enabled = True
        AnswerA.Enabled = True
        AnswerB.Enabled = True
        AnswerC.Enabled = True
        AnswerD.Enabled = True
        GenerateQuestion()
    End Sub

    Private Sub gameEnds()
        Timer1.Enabled = False
        AnswerA.Enabled = False
        AnswerB.Enabled = False
        AnswerC.Enabled = False
        AnswerD.Enabled = False
        If gameLevel < 30 Then
            MessageBox.Show("You Lose", "Lose", MessageBoxButtons.OK)
        Else
            MessageBox.Show("You Win", "Win", MessageBoxButtons.OK)
        End If
        Button1.Enabled = True
        QuestionLabel.Text = "Math Quiz"
        gameLevel = 0
        LevelLabel.Text = 0
    End Sub

    Private Function randomNumber(upperbound As Integer, lowerbound As Integer)
        Dim xResult As Integer = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
        Return xResult
    End Function

    Private Sub GenerateMarks(levels As Integer)
        Dim randomMarks As Integer = randomNumber(levels, 0)
        Select Case randomMarks
            Case 0 To 10
                marks = "+"
            Case 11 To 20
                marks = "-"
            Case 21 To 30
                marks = "*"
            Case 31 To 40
                marks = "/"
            Case Else
                marks = "+"
        End Select
    End Sub

    Private Sub GenerateNumbers()
        Select Case gameLevel
            Case 0 To 10
                'easy level
                GenerateMarks(20)
                q1 = randomNumber(100, 1)
                q2 = randomNumber(q1, 1)
            Case 11 To 20
                'moderate level
                GenerateMarks(30)
                q1 = randomNumber(50, 10)
                q2 = randomNumber(50, 10)
            Case 21 To 30
                'hard level
                GenerateMarks(40)
                q1 = randomNumber(100, 10)
                q2 = randomNumber(100, 10)
        End Select

    End Sub

    Private Sub GenerateQuestion()
        GenerateNumbers()
        Select Case marks
            Case "+"
                answer = q1 + q2
            Case "-"
                answer = q1 - q2
            Case "*"
                answer = q1 * q2
            Case "/"
                answer = randomNumber(20, 10)
                q1 = q2 * answer
        End Select
        QuestionLabel.Text = q1 & marks & q2 & "="
        Dim answerLocation As Integer = randomNumber(4, 1)
        Select Case answerLocation
            Case 1
                AnswerA.Text = answer
                AnswerB.Text = randomNumber(answer * 2, answer + 10)
                AnswerC.Text = randomNumber(answer - 1, 0)
                AnswerD.Text = randomNumber(answer + 110, answer - 0)
            Case 2
                AnswerB.Text = answer
                AnswerA.Text = randomNumber(answer * 2, answer + 10)
                AnswerC.Text = randomNumber(answer - 1, 0)
                AnswerD.Text = randomNumber(answer + 110, answer - 0)
            Case 3
                AnswerC.Text = answer
                AnswerB.Text = randomNumber(answer * 2, answer + 10)
                AnswerA.Text = randomNumber(answer, 0)
                AnswerD.Text = randomNumber(answer + 10, answer)
            Case 4
                AnswerD.Text = answer
                AnswerB.Text = randomNumber(answer * 2, answer + 10)
                AnswerC.Text = randomNumber(answer - 1, 0)
                AnswerA.Text = randomNumber(answer + 100, answer)
        End Select
    End Sub

    Private Sub NextLevel()
        If gameLevel < 30 Then
            gameLevel = gameLevel + 1
            LevelLabel.Text = gameLevel
            TimeLabel.Text += 20
            GenerateQuestion()
        Else
            gameEnds()
        End If
    End Sub

    Private Sub AnswerCheck(guess As Integer)
        If guess = answer Then
            NextLevel()
        Else
            gameEnds()
        End If
    End Sub

    Private Sub AnswerA_Click(sender As System.Object, e As System.EventArgs) Handles AnswerA.Click
        AnswerCheck(AnswerA.Text)
    End Sub

    Private Sub AnswerB_Click(sender As System.Object, e As System.EventArgs) Handles AnswerB.Click
        AnswerCheck(AnswerB.Text)
    End Sub

    Private Sub AnswerC_Click(sender As System.Object, e As System.EventArgs) Handles AnswerC.Click
        AnswerCheck(AnswerC.Text)
    End Sub

    Private Sub AnswerD_Click(sender As System.Object, e As System.EventArgs) Handles AnswerD.Click
        AnswerCheck(AnswerD.Text)
    End Sub
End Class
